package second.codeTop.package2;

public class LC912 {
    public static void main(String[] args) {

    }

    // 排序数组，使用快排
    public static int[] sortArray(int[] nums) {
        quickSort(nums, 0, nums.length-1);
        return nums;
    }

    public static void quickSort(int[] nums, int left, int right) {
        if (left >= right) {
            return;
        }
        int i = left;
        int j = right;
        int temp = nums[i];
        while (i < j) {
            while (i < j && nums[j] > temp) {
                j--;
            }
            if (i < j) swap(nums, i, j);
            while (i < j && nums[i] <= temp) {
                i++;
            }
            if (i < j) swap(nums, i, j);
        }
        quickSort(nums, left, i-1);
        quickSort(nums, i+1, right);
    }

    public static void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}
